/**********************************************************************************************************
** 驱动文件：	dianli-E_9SY.c
** 驱动类型：	dianli
** 设备名称：	电力监测
** 设备型号：	E_9SY
** 设备品牌：	融智云
** 设备协议：	Modbus-RTU
** 驱动描述：	适用于E-9SY系列具有可编程测量、显示、数字通讯和电能脉冲输出等多功能智能仪表。
** 生成日期：	2019-07-18 16:39:03
**********************************************************************************************************/
#include "Includes.h"


/**********************************************************************************************************
* 定义设备数据点变量结构
**********************************************************************************************************/
typedef struct{
	Var Ua;										//A相电压
	Var Ub;										//B相电压
	Var Uc;										//C相电压
	Var Ia;										//A相电流
	Var Ib;										//B相电流
	Var Ic;										//C相电流
	Var TotalActivePower;						//总有功功率
	Var Pa;										//A相有功功率
	Var Pb;										//B相有功功率
	Var Pc;										//C相有功功率
	Var TotalReactivePower;						//总无功功率
	Var Qa;										//A相无功功率
	Var Qb;										//B相无功功率
	Var Qc;										//C相无功功率
	Var TotalApparentPower;						//总视在功率
	Var Sa;										//A相视在功率
	Var Sb;										//B相视在功率
	Var Sc;										//C相视在功率
	Var TotalPowerFactor;						//总功率因数
	Var COSa;									//A相功率因数
	Var COSb;									//B相功率因数
	Var COSc;									//C相功率因数
	Var Hz;										//频率
	Var TotalActiveEnergy;						//总有功电能
	Var InductiveReactiveEnergy;				//感性无功电能
	Var CapacitiveReactiveEnergy;				//容性无功电能
}Data_Struct;									//定义变量结构


/**********************************************************************************************************
* 设备数据点变量属性声明
**********************************************************************************************************/
const static Var_Property_Def var_attr[] = {
	{
		FLOAT,
		0,
		READONLY,
		"Ua",
		"A相电压",
		"V",
		"A相电压",
		0,
		0,
		"A相电压过高",
		"A相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Ub",
		"B相电压",
		"V",
		"B相电压",
		0,
		0,
		"B相电压过高",
		"B相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Uc",
		"C相电压",
		"V",
		"C相电压",
		0,
		0,
		"C相电压过高",
		"C相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Ia",
		"A相电流",
		"A",
		"A相电流",
		0,
		0,
		"A相电流过高",
		"A相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Ib",
		"B相电流",
		"A",
		"B相电流",
		0,
		0,
		"B相电流过高",
		"B相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Ic",
		"C相电流",
		"A",
		"C相电流",
		0,
		0,
		"C相电流过高",
		"C相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalActivePower",
		"总有功功率",
		"KW",
		"总有功功率",
		0,
		0,
		"总有功功率过高",
		"总有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Pa",
		"A相有功功率",
		"KW",
		"A相有功功率",
		0,
		0,
		"A相有功功率过高",
		"A相有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Pb",
		"B相有功功率",
		"KW",
		"B相有功功率",
		0,
		0,
		"B相有功功率过高",
		"B相有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Pc",
		"C相有功功率",
		"KW",
		"C相有功功率",
		0,
		0,
		"C相有功功率过高",
		"C相有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalReactivePower",
		"总无功功率",
		"KVar",
		"总无功功率",
		0,
		0,
		"总无功功率过高",
		"总无功功率过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"Qa",
		"A相无功功率",
		"KVar",
		"A相无功功率",
		0,
		0,
		"A相无功功率过高",
		"A相无功功率过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"Qb",
		"B相无功功率",
		"KVar",
		"B相无功功率",
		0,
		0,
		"B相无功功率过高",
		"B相无功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Qc",
		"C相无功功率",
		"KVar",
		"C相无功功率",
		0,
		0,
		"C相无功功率过高",
		"C相无功功率过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalApparentPower",
		"总视在功率",
		"VA",
		"总视在功率",
		0,
		0,
		"总视在功率过高",
		"总视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Sa",
		"A相视在功率",
		"KVA",
		"A相视在功率",
		0,
		0,
		"A相视在功率过高",
		"A相视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Sb",
		"B相视在功率",
		"KVA",
		"B相视在功率",
		0,
		0,
		"B相视在功率过高",
		"B相视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Sc",
		"C相视在功率",
		"KVA",
		"C相视在功率",
		0,
		0,
		"C相视在功率过高",
		"C相视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalPowerFactor",
		"总功率因数",
		"",
		"总功率因数",
		0,
		0,
		"总功率因数过高",
		"总功率因数过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"COSa",
		"A相功率因数",
		"",
		"A相功率因数",
		0,
		0,
		"A相功率因数过高",
		"A相功率因数过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"COSb",
		"B相功率因数",
		"",
		"B相功率因数",
		0,
		0,
		"B相功率因数过高",
		"B相功率因数过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"COSc",
		"C相功率因数",
		"",
		"C相功率因数",
		0,
		0,
		"C相功率因数过高",
		"C相功率因数过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"Hz",
		"频率",
		"Hz",
		"A相电压频率",
		0,
		0,
		"A相电压频率过高",
		"A相电压频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalActiveEnergy",
		"总有功电能",
		"KW•h",
		"总有功电能",
		0,
		0,
		"总有功电能过高",
		"总有功电能过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"InductiveReactiveEnergy",
		"感性无功电能",
		"KVar•h",
		"感性无功电能",
		0,
		0,
		"感性无功电能过高",
		"感性无功电能过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"CapacitiveReactiveEnergy",
		"容性无功电能",
		"KVar•h",
		"容性无功电能",
		0,
		0,
		"容性无功电能过高",
		"容性无功电能过低",
		0.000,
		0.000
	}
};


/**********************************************************************************************************
* 函数名称： static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
* 函数说明： 生成获取传感器数据指令
* 输入参数： 指令索引，传感器地址，变量结构体指针,以及指令缓存区指针,指令长度
* 返回参数： 无
**********************************************************************************************************/
static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
{
	switch(index)
	{
		case 0:
			cmd[0] = addr;
			cmd[1] = 0x04;
			cmd[2] = 0x00;								
			cmd[3] = 0x02;
			cmd[4] = 0x00;	
			cmd[5] = 0x39;
			*len = CRC16_Padding(cmd, 6);
			break;
		default:
			break;
	}
}


/**********************************************************************************************************
* 函数名称： static int parse_fun(short index, int addr, void *data, char *buf, short len)
* 函数说明： 解析传感器响应的数据，并写入数据库
* 输入参数： 解析索引,传感器地址，变量结构体指针，以及响应数据缓存取指针，响应数据长度
* 返回参数： 返回解析结果，返回0成功，返回1失败
**********************************************************************************************************/
static int parse_fun(short index, int addr, void *data, char *buf, short len)
{
	union{
		unsigned short	dat16;
      	unsigned char  	dat8[2];
    }DAT16_8;						   //数据类型转换	
    union{
		unsigned int	dat32;
		unsigned char   dat8[4];
    }DAT32_8;							//数据类型转换		
	Data_Struct 	*d = (Data_Struct*)data;
	unsigned short	PT , CT, Flag;
	if(buf!=NULL && buf[0]==addr && len>0 )
	{	
		if(CRC16_Check(buf,len))
		{
			switch(index)
			{
				case 0:
					DAT16_8.dat8[1] = buf[3];
					DAT16_8.dat8[0] = buf[4];
					PT = DAT16_8.dat16;
					DAT16_8.dat8[1] = buf[5];
					DAT16_8.dat8[0] = buf[6];				
					CT = DAT16_8.dat16;
					DAT16_8.dat8[1] = buf[39];
					DAT16_8.dat8[0] = buf[40];
					d->Ua.value.fnum = DAT16_8.dat16*PT*0.1;
					DAT16_8.dat8[1] = buf[41];
					DAT16_8.dat8[0] = buf[42];
					d->Ub.value.fnum = DAT16_8.dat16*PT*0.1;
					DAT16_8.dat8[1] = buf[43];
					DAT16_8.dat8[0] = buf[44];
					d->Uc.value.fnum = DAT16_8.dat16*PT*0.1;

					DAT16_8.dat8[1] = buf[51];
					DAT16_8.dat8[0] = buf[52];
					d->Ia.value.fnum = DAT16_8.dat16*CT*0.0001;
					DAT16_8.dat8[1] = buf[53];
					DAT16_8.dat8[0] = buf[54];
					d->Ib.value.fnum = DAT16_8.dat16*CT*0.0001;
					DAT16_8.dat8[1] = buf[55];
					DAT16_8.dat8[0] = buf[56];
					d->Ic.value.fnum = DAT16_8.dat16*CT*0.0001;
					
					DAT16_8.dat8[1] = buf[57];
					DAT16_8.dat8[0] = buf[58];				
					Flag = DAT16_8.dat16;

					DAT16_8.dat8[1] = buf[59];
					DAT16_8.dat8[0] = buf[60];
					d->Pa.value.fnum = Flag&0x0001?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[61];
					DAT16_8.dat8[0] = buf[62];
					d->Pb.value.fnum = Flag&0x0002?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[63];
					DAT16_8.dat8[0] = buf[64];
					d->Pc.value.fnum = Flag&0x0004?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;	
					DAT16_8.dat8[1] = buf[65];
					DAT16_8.dat8[0] = buf[66];
					d->TotalActivePower.value.fnum = Flag&0x0008?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;

					DAT16_8.dat8[1] = buf[67];
					DAT16_8.dat8[0] = buf[68];
					d->Qa.value.fnum = Flag&0x0010?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[69];
					DAT16_8.dat8[0] = buf[70];
					d->Qb.value.fnum = Flag&0x0020?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[71];
					DAT16_8.dat8[0] = buf[72];
					d->Qc.value.fnum = Flag&0x0040?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[73];
					DAT16_8.dat8[0] = buf[74];
					d->TotalReactivePower.value.fnum = Flag&0x0080?DAT16_8.dat16*PT*CT*(-0.0001):DAT16_8.dat16*PT*CT*0.0001;

					DAT16_8.dat8[1] = buf[75];
					DAT16_8.dat8[0] = buf[76];
					d->Sa.value.fnum = DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[77];
					DAT16_8.dat8[0] = buf[78];
					d->Sb.value.fnum = DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[79];
					DAT16_8.dat8[0] = buf[80];
					d->Sc.value.fnum = DAT16_8.dat16*PT*CT*0.0001;
					DAT16_8.dat8[1] = buf[81];
					DAT16_8.dat8[0] = buf[82];
					d->TotalApparentPower.value.fnum = DAT16_8.dat16*PT*CT*0.0001;

					DAT16_8.dat8[1] = buf[83];
					DAT16_8.dat8[0] = buf[84];
					d->COSa.value.fnum = Flag&0x0100?DAT16_8.dat16*(-0.001):DAT16_8.dat16*0.001;
					DAT16_8.dat8[1] = buf[85];
					DAT16_8.dat8[0] = buf[86];
					d->COSb.value.fnum = Flag&0x0200?DAT16_8.dat16*(-0.001):DAT16_8.dat16*0.001;
					DAT16_8.dat8[1] = buf[87];
					DAT16_8.dat8[0] = buf[88];
					d->COSc.value.fnum = Flag&0x0400?DAT16_8.dat16*(-0.001):DAT16_8.dat16*0.001;		
					DAT16_8.dat8[1] = buf[89];
					DAT16_8.dat8[0] = buf[90];
					d->TotalPowerFactor.value.fnum = Flag&0x0800?DAT16_8.dat16*(-0.001):DAT16_8.dat16*0.001;
					
					DAT16_8.dat8[1] = buf[91];
					DAT16_8.dat8[0] = buf[92];
					d->Hz.value.fnum = DAT16_8.dat16*0.01;

					DAT32_8.dat8[3] = buf[93];
					DAT32_8.dat8[2] = buf[94];
					DAT32_8.dat8[1] = buf[95];
					DAT32_8.dat8[0] = buf[96];
					DAT16_8.dat8[1] = buf[97];
					DAT16_8.dat8[0] = buf[98];
					d->TotalActiveEnergy.value.fnum = DAT32_8.dat32+DAT16_8.dat16*0.001;

					DAT32_8.dat8[3] = buf[105];
					DAT32_8.dat8[2] = buf[106];
					DAT32_8.dat8[1] = buf[107];
					DAT32_8.dat8[0] = buf[108];
					DAT16_8.dat8[1] = buf[109];
					DAT16_8.dat8[0] = buf[110];
					d->InductiveReactiveEnergy.value.fnum = DAT32_8.dat32+DAT16_8.dat16*0.001;

					DAT32_8.dat8[3] = buf[111];
					DAT32_8.dat8[2] = buf[112];
					DAT32_8.dat8[1] = buf[113];
					DAT32_8.dat8[0] = buf[114];
					DAT16_8.dat8[1] = buf[115];
					DAT16_8.dat8[0] = buf[116];
					d->CapacitiveReactiveEnergy.value.fnum = DAT32_8.dat32+DAT16_8.dat16*0.001;
					break;
				default:
					break;
			}
			return 0;
		}
	}
	return 1;
}


/**********************************************************************************************************
* 函数名称： void dianli_E_9SY_Registration(void)
* 函数说明： 电力监测驱动注册
* 输入参数： 无
* 返回参数： 无
**********************************************************************************************************/
void dianli_E_9SY_Registration(void)
{
	DeviceRegWithResTimeout(
		"dianli",												//设备类型
		"电力监测",												//设备名称（导航栏默认显示名称）
		"E_9SY",												//设备型号
		"适用于E-9SY系列具有可编程测量、显示、数字通讯和电能脉冲输出等多功能智能仪表。",//驱动描述
		var_attr,												//变量属性声明
		sizeof(Data_Struct),									//变量结构体空间大小
		cmd_fun,												//发送指令生成函数
		parse_fun,												//数据解析函数
		250														//设备响应超时时间
	);
}

